package com.sunjob.mroresumeproject.partscontroller;

import com.sunjob.common.domain.mroresume.CKParts;
import com.sunjob.common.domain.mroresume.TFBus;
import com.sunjob.common.domain.mroresume.WHBus;
import com.sunjob.common.result.Result;
import com.sunjob.common.util.MROUtils;
import com.sunjob.mroresumeproject.service.CKPartsServcie;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;

@RestController
@RequestMapping("/CKparts")
@CrossOrigin
@Api(tags = "部件定检履历")
public class CKPartsController {

    @Autowired
    CKPartsServcie ckPartsServcie;

    @RequestMapping("/getpages")
    public Result getpages(@RequestParam(value = "pages2")String pages2){

        int page2=Integer.parseInt(pages2);
        if (page2<1)
        {
            page2=1;

        }

        int size=5;
        //部件定检履历查出部件定检记录总条数
        int count=ckPartsServcie.querycount();
        //求总页数
        int pageCount = count %size == 0 ? count / size : count / size + 1;

        if(page2>pageCount)
        {
            page2=pageCount;
        }

        //第一页放五条数据
        int pages=(page2-1)*size;
        List<CKParts> list=ckPartsServcie.queryCKparts(pages,size);


        //map存入不同类型对象
        Map map=new HashMap();
        map.put("page2",page2);
        map.put("count",count);
        map.put("pageCount",pageCount);
        map.put("list",list);


        //result对象返回数据到前端
        Result result=new Result();
        result.setData(map);
        return result;

    }


    //部件定检履历菜单查询
    @RequestMapping("/selectCKparts")
    @ApiOperation(value = "分页")
    public Result selectCKparts(){
        int page2=1;
        int size=5;

        //部件定检履历查出部件定检记录总条数
        int count=ckPartsServcie.querycount();
         //求总页数
        int pageCount = count %size == 0 ? count / size : count / size + 1;



        //第一页放五条数据
        int pages=(page2-1)*size;
        List<CKParts> list=ckPartsServcie.queryCKparts(pages,size);

        //map存入不同类型对象
        Map map=new HashMap();
        map.put("page2",page2);
        map.put("count",count);
        map.put("pageCount",pageCount);
        map.put("list",list);

        //result对象返回数据到前端
        Result result=new Result();
        result.setData(map);
        return result;
    }

    //搜索框输入条件查询
    @RequestMapping("/selectCKcontent")
    @ApiOperation(value = "条件查询")
    public Result selectCKcontent(@ApiParam(value = "checkid",required = false) String checkid){
        //搜索框条件查询可以是部件定检编号或者部件名称
        List<CKParts> list=ckPartsServcie.queryCKparts2(checkid);

        //result返回查询出的部件定检履历记录
        Result result=new Result();
        result.setData(list);
        System.out.println(result);
        return result;


    }

//新增按钮，部件定检履历新增
    @RequestMapping("/insertCKparts")
    @ApiOperation(value = "新增")
    public void insertCKparts(CKParts ckParts){
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDateTime ldt = LocalDate.parse(ckParts.getStarttime2(),df).atStartOfDay();
        LocalDateTime ldt2 = LocalDate.parse(ckParts.getEndtime2(),df).atStartOfDay();
        if(ckParts.getFailreason()==""){
            ckParts.setFailreason("无");
            ckParts.setCKstate("完成");

        }
        else {

            ckParts.setCKstate("失败");


        }
        ckParts.setStarttime(ldt);
        ckParts.setEndtime(ldt2);
        ckParts.setStatus(1);
        ckParts.setCheckid(MROUtils.nextId());
        ckPartsServcie.insertCKparts(ckParts);

    }


    @RequestMapping("/insertCKparts2")
    public void insertTFBus(CKParts ckParts) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");

        Date date = sdf.parse(ckParts.getStarttime2().split("\"")[1]);
//        System.out.println(date);
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();

        ckParts.setStarttime(instant.atZone(zoneId).toLocalDateTime());

        if (ckParts.getFailreason()==""){
            ckParts.setFailreason("无");
            ckParts.setCKstate("完成");
        }
        else {
            ckParts.setCKstate("失败");
        }
        Date date1 = new Date();
        Instant instant1 = date1.toInstant();
        ZoneId zoneId1 = ZoneId.systemDefault();

        ckParts.setEndtime(instant1.atZone(zoneId1).toLocalDateTime());
        ckParts.setStatus(1);


        ckPartsServcie.insertCKparts(ckParts);
    }



    //编辑按钮，根据checkid查询出当前id的数据
    @RequestMapping("returnCKparts")
    @ApiOperation(value = "查询")
    public Result returnCKparts(@RequestParam("checkid")Long checkid){
        List<CKParts>ckParts=ckPartsServcie.queryCKpartsone(checkid);
        Result result=new Result();
        result.setData(ckParts);
        return result;
    }

    //对根据checkid查询出的部件定检履历数据做修改
    @RequestMapping("/updateCKparts")
    @ApiOperation(value = "修改")
    public Result updateCKparts(@RequestParam(value = "checkid",required = false)String checkid,
                                @RequestParam(value = "CKcontent",required = false)String CKcontent,
                                @RequestParam(value = "partstypes",required = false)String partstypes,
                                @RequestParam(value = "management",required = false)String management,
                                @RequestParam(value = "partsinitial",required = false)String partsinitial,
                                @RequestParam(value = "partsend",required = false)String partsend,
                                @RequestParam(value = "failreson",required = false)String  failreason,
                                @RequestParam(value = "money",required = false)String money,
                                @RequestParam(value = "starttime",required = false)String starttime,
                                @RequestParam(value = "endtime",required = false) String endtime
                                ){

        ckPartsServcie.updateCKparts(checkid,CKcontent,partstypes,management,partsinitial,partsend,failreason,money,starttime,endtime);
        //返回修改后的数据列表
        List<CKParts>ckParts=ckPartsServcie.queryCKparts5();
        Result result=new Result();
        result.setData(ckParts);
        return result;
    }

    //根据checkid删除修改这条记录的状态
    @RequestMapping("/deleteCKparts")
    @ApiOperation(value = "删除")
    public Result deleteCKparts(@RequestParam("checkid")String checkid){
        //修改这条记录状态，0为消失，1为存在
        ckPartsServcie.deleteCKparts(checkid);

        //删除后查询部件定检履历记录
        List<CKParts>ckParts=ckPartsServcie.queryCKparts5();
        Result result=new Result();
        result.setData(ckParts);
        return result;
    }


    @RequestMapping("/deleteCKParts2")
    public void deleteWHBus2(String checkid2){
        System.out.println(checkid2);
        String[] a= checkid2.split(",");
        String[] c=  a[0].split("\\[");
        System.out.println(c[0]);
        System.out.println(c.length);
        String[] d = a[a.length-1].split("\\]");
        for (int i = 0; i < a.length; i++) {
            if (i==0){
                a[0]=c[1];

            }
            if (i==a.length-1){
                a[a.length-1]=d[0];
            }
            ckPartsServcie.deleteCKpartsone(a[i]);
        }
    }

}
